package com.zw.knowlege.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zw.common.constant.NumberConst;
import com.zw.common.response.ResponseResult;
import com.zw.knowlege.common.StaticValue;
import com.zw.knowlege.mapper.CollectRecordMapper;
import com.zw.knowlege.mapper.ExperienceBaseMapper;
import com.zw.knowlege.pojo.CollectRecordPojo;
import com.zw.knowlege.pojo.ExperiencePojo;
import com.zw.knowlege.service.CollectRecordService;
import com.zw.knowlege.service.ParamParseService;
import com.zw.knowlege.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class CollectRecordServiceImpl implements CollectRecordService{

    @Autowired
    private CollectRecordMapper collectRecordMapper;

    @Autowired
    private ExperienceBaseMapper experienceBaseMapper;

    @Autowired
    private ParamParseService paramParseService;

    /**
     * 收藏记录
     * @param type
     * @return
     */
    @Override
    public ResponseResult collectOneRecord(CollectRecordPojo collectRecordPojo) {
        int resultCount = collectRecordMapper.collectOneExperience(collectRecordPojo);
        if(resultCount > NumberConst.NUM_0){
            // 经验册需要去信息表将收藏次数修改
            if(StaticValue.EXPERIENCE.equals(collectRecordPojo.getType())){
                experienceBaseMapper.updateCollectCount(collectRecordPojo.getRecordid(), "collect");
            }
            return ResponseResult.success();
        }
        return ResponseResult.failure();
    }

    @Override
    public PageInfo<ExperiencePojo> queryMyCollectExperience(JSONObject paramJson) {
        Map<String, Object> paramMap;
        if(CommonUtils.nullOrEmpty(paramJson.toJSONString())){
            paramMap = new HashMap<>();
            paramMap.put(StaticValue.PAGENUM, 1);
            paramMap.put(StaticValue.PAGESIZE, 10);
        }else{
            paramMap = (Map<String, Object>)paramParseService.parseParam(paramJson, Map.class,
                    new String[]{StaticValue.KEYWORD, StaticValue.EXPERIENCE_DEPTTYPE, StaticValue.EXPERIENCE_CLASSINFO,
                            StaticValue.STATUS, StaticValue.EXPERIENCE_USERINFO_USERID, StaticValue.PAGENUM, StaticValue.PAGESIZE});
        }
        List<CollectRecordPojo> collectRecords = collectRecordMapper.queryMyCollectRecord(paramMap.get(StaticValue.EXPERIENCE_USERINFO_USERID).toString());
        if(collectRecords.isEmpty()){
            log.info("用户【{}】没有收藏记录", paramMap.get(StaticValue.EXPERIENCE_USERINFO_USERID));
            return new PageInfo<>();
        }
        PageHelper.startPage(Integer.parseInt(paramMap.get(StaticValue.PAGENUM).toString()), Integer.parseInt(paramMap.get(StaticValue.PAGESIZE).toString()));
        List<ExperiencePojo> myCollectRecordList = experienceBaseMapper.queryExperienceByInId(collectRecords);
        log.info("用户【{}】收藏记录数【{}】", paramMap.get(StaticValue.EXPERIENCE_USERINFO_USERID), myCollectRecordList.size());
        if (CollectionUtil.isNotEmpty(myCollectRecordList)) {
            return new PageInfo<>(myCollectRecordList);
        } else {
            return new PageInfo<>();
        }
    }

    @Override
    public ResponseResult cancelCollectRecord(CollectRecordPojo collectRecordPojo) {
        try{
            collectRecordMapper.cancelCollectExperience(collectRecordPojo.getUserid(), collectRecordPojo.getRecordid());
            experienceBaseMapper.updateCollectCount(collectRecordPojo.getRecordid(), "cancel");
            return ResponseResult.success();
        }catch (Exception e){
            return ResponseResult.failure(e.getCause().toString());
        }
    }

    @Override
    public ResponseResult doCollectProcess(JSONObject paramJson) {
        CollectRecordPojo collectRecordPojo = (CollectRecordPojo)paramParseService.parseParam(paramJson, CollectRecordPojo.class, null);
        if(null == collectRecordPojo){
            log.info("【{}】经验收藏操作出现异常");
            return ResponseResult.failure("【" + paramJson + "】经验收藏操作出现异常");
        }
        if("collect".equals(collectRecordPojo.getAction())){
            return collectOneRecord(collectRecordPojo);
        }else if("cancel".equals(collectRecordPojo.getAction())){
            return cancelCollectRecord(collectRecordPojo);
        }else{
            return ResponseResult.failure("传入的动作类型参数不对，请检查");
        }
    }
}
